package com.zjh.photoshopstudy.app.main;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;

import com.zjh.photoshopstudy.Model.VideoEntity;
import com.zjh.photoshopstudy.R;
import com.zjh.photoshopstudy.Utils.Data;
import com.zjh.photoshopstudy.app.video.VideoHttp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment implements TabLayout.OnTabSelectedListener, View.OnClickListener, OnGetVideoListener {
    private TabLayout mTypeLayout;
    private TabLayout mLevelLayout;
    private TabLayout tabLayout;
    private EditText mSearchEdit;
    private ImageButton mSearchBtn;
    private RecyclerView mMainRecyclerView;
    MainListAdapter adapter;
    private VideoHttp http;

    public MainFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        http = new VideoHttp();
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mTypeLayout = view.findViewById(R.id.type_tab);
        mLevelLayout = view.findViewById(R.id.level_tab);
        tabLayout = view.findViewById(R.id.tabLayout);
        mSearchEdit = view.findViewById(R.id.search_edit);
        mSearchBtn = view.findViewById(R.id.search_btn);
        mMainRecyclerView = view.findViewById(R.id.main_list);
        for (String type : Data.TYPES) {
            mTypeLayout.addTab(mTypeLayout.newTab().setText(type));
        }
        for (String level : Data.LEVELS) {
            mLevelLayout.addTab(mLevelLayout.newTab().setText(level));
        }
        tabLayout.addTab(tabLayout.newTab().setText("时间"));
        tabLayout.addTab(tabLayout.newTab().setText("热度"));
        mTypeLayout.addOnTabSelectedListener(this);
        mLevelLayout.addOnTabSelectedListener(this);
        tabLayout.addOnTabSelectedListener(this);
        getVideoList();
        mSearchBtn.setOnClickListener(this);
    }


    private void getVideoList() {
        String string = mSearchEdit.getText().toString().trim();
        int type = mTypeLayout.getSelectedTabPosition();
        int level = mLevelLayout.getSelectedTabPosition();
        int order = tabLayout.getSelectedTabPosition();
        Map<String, Object> map = new HashMap<>();
        if (type != 0) {
            map.put("vType", type);
        }
        if (level != 0) {
            map.put("vLevel", level);
        }
        if (!string.isEmpty()) {
            map.put("vTitle", string);
        }
        if (order != 0) {
            map.put("orderByLike", 1);
        }
        Map<String, Object> map2 = new HashMap<>();
        map2.put("fastQueryParameters", map);
        http.getVideoList(this, map);

    }

    @Override
    public void getVideoListSuccess(List<VideoEntity> list) {
        adapter = new MainListAdapter(list, getContext());
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        mMainRecyclerView.setLayoutManager(layoutManager);
        mMainRecyclerView.setAdapter(adapter);
    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        getVideoList();
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_btn:
                getVideoList();
                break;
        }
    }

}
